/*
 * Copyright (c) 2020 libinal's testing project.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * imitations under the License.
 */

package com.platform.comm.repository;

import com.querydsl.core.Tuple;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.JPQLQuery;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.NoRepositoryBean;

import javax.persistence.EntityManager;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

/**
 * Custom base repository interface
 *
 * @author coder
 */
@NoRepositoryBean
public interface CustomBaseRepository<T, ID> extends JpaRepository<T, ID>, QuerydslPredicateExecutor<T> {

    /**
     * Find single.
     *
     * @param predicate 条件
     * @param consumer  JPQLQuery
     * @return Optional
     */
    Optional<T> findOne(Predicate predicate, Consumer<JPQLQuery<?>> consumer);

    /**
     * Find Single.
     *
     * @param predicate   Predicate
     * @param expressions Expression
     * @return Optional
     */
    Optional<Tuple> findOne(Predicate predicate, Expression<?>... expressions);

    /**
     * Find Single.
     *
     * @param predicate   Predicate
     * @param consumer    Consumer
     * @param expressions Expression
     * @return Optional
     */
    Optional<Tuple> findOne(Predicate predicate, Consumer<JPQLQuery<?>> consumer, Expression<?>... expressions);

    /**
     * Find ALL
     *
     * @param predicate Predicate
     * @param consumer  Consumer
     * @return List
     */
    List<T> findAll(Predicate predicate, Consumer<JPQLQuery<?>> consumer);

    /**
     * Find All.
     *
     * @param predicate   Predicate
     * @param expressions Expression
     * @return List
     */
    List<Tuple> findAll(Predicate predicate, Expression<?>... expressions);

    /**
     * Entity Manager
     *
     * @return EntityManager
     */
    EntityManager entityManager();

    /**
     * 查询所有.
     *
     * @param predicate   条件
     * @param sort        排序
     * @param expressions 投影
     * @return List
     */
    List<Tuple> findAll(Predicate predicate, Sort sort, Expression<?>... expressions);

    /**
     * 查询所有
     *
     * @param predicate   条件
     * @param consumer    条件
     * @param expressions 投影
     * @return List
     */
    List<Tuple> findAll(Predicate predicate, Consumer<JPQLQuery<?>> consumer, Expression<?>... expressions);

    /**
     * 查询所有
     *
     * @param consumer    条件
     * @param expressions 投影
     * @return List
     */
    List<Tuple> findAll(Consumer<JPQLQuery<?>> consumer, Expression<?>... expressions);

    /**
     * 查询所有
     *
     * @param consumer    条件
     * @param sort        排序
     * @param expressions 投影
     * @return List
     */
    List<Tuple> findAll(Consumer<JPQLQuery<?>> consumer, Sort sort, Expression<?>... expressions);

    /**
     * 查询所有
     *
     * @param predicate   条件
     * @param consumer    条件
     * @param sort        排序
     * @param expressions 投影
     * @return List
     */
    List<Tuple> findAll(Predicate predicate, Consumer<JPQLQuery<?>> consumer, Sort sort, Expression<?>... expressions);

    /**
     * 查询所有
     *
     * @param predicate 条件
     * @param pageable  分页参数
     * @param consumer  条件
     * @return 分页列表
     */
    Page<T> findAll(Predicate predicate, Pageable pageable, Consumer<JPQLQuery<?>> consumer);

    /**
     * 查询所有
     *
     * @param predicate   条件
     * @param pageable    分页参数
     * @param expressions 投影
     * @return 分页列表
     */
    Page<Tuple> findAll(Predicate predicate, Pageable pageable, Expression<?>... expressions);

    /**
     * 查询所有
     *
     * @param predicate   条件
     * @param pageable    分页参数
     * @param consumer    条件
     * @param expressions 投影
     * @return 分页列表
     */
    Page<Tuple> findAll(Predicate predicate, Pageable pageable, Consumer<JPQLQuery<?>> consumer, Expression<?>... expressions);
}
